package cn.youyeku.sockettransmission.CLIENT;



import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import cn.youyeku.sockettransmission.utils.LogUtils;

/**
 * Created by youye on 17-6-8.
 */
public abstract class BaseClientSocket {

    protected int PORT;
    protected String IP_ADDRESS;
    protected Socket mSocket;
    protected String mSocketType;
    protected DataInputStream mInRead;
    protected DataOutputStream mOutWrite;

    public BaseClientSocket(int PORT, String IP_ADDRESS, String mSocketType) {
        this.PORT = PORT;
        this.IP_ADDRESS = IP_ADDRESS;
        this.mSocketType = mSocketType;

    }

    public void init(){
        try {
            mSocket = new Socket(IP_ADDRESS, PORT);
            mSocket.setReceiveBufferSize(1024*32);
            mSocket.setSendBufferSize(1024*32);
            mInRead = new DataInputStream(mSocket.getInputStream());
            mOutWrite = new DataOutputStream(mSocket.getOutputStream());
            //发送服务器客户端类型
            mOutWrite.writeUTF(mSocketType);

        } catch (IOException e) {
            LogUtils.w(ConstantClient.ERROR_CLIENT_EXIT);
            e.printStackTrace();
        }

        //todo init get
        /**
         * while循环 接收请求  文本流请求文本 文件请求文件
         *
         */
          while(true){

              try {
                  onGetRecycle();
              } catch (IOException e) {
                  LogUtils.w("循环错误-");
                  System.exit(-1);
                  e.printStackTrace();
              }
          }




    }

    protected abstract void onGetRecycle() throws IOException;

}
